import {App, Provide,Get } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { Application } from 'egg'

/**
 * 微信小程序云数据库获取
 */
@Provide()
@CoolController('/api')
export class weixinController extends BaseController {
  /**
   * 其他接口
   */
   @App()
   app: Application;

  @Get('/weixin')
  async other() {
     const data = await this.app.curl('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxeeb6eaa621409d73&secret=0c61d4dc98e9c2178726f160bf17faa6',{
      dataType: 'json',
    });
   // console.log(data.data.access_token)
    // 根据返回的token 去获取数据库内容
   const resdatabase = await this.app.curl('https://api.weixin.qq.com/tcb/databasequery?access_token='+data.data.access_token,{
        method: 'POST',
        // 通过 contentType 告诉 HttpClient 以 JSON 格式发送
        contentType: 'json',
        data: {
          "env":"dfaytest-ry4p6",
          "query": "db.collection(\"order\").get()"
        },
        // 明确告诉 HttpClient 以 JSON 格式处理返回的响应 body
        dataType: 'json',
    })
    resdatabase.code = 1000
     return resdatabase;
  }
}